<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 商品列表 -->
    <el-card class="box-card">
      <!-- 搜索和添加用户 -->
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" @clear="getGoodsList" v-model="queryData.query" class="input-with-select" clearable>
              <el-button slot="append" @click="getGoodsList" icon="el-icon-search"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
             <el-button type="primary" @click="$router.push('goods/add')">添加商品</el-button>
          </el-col>
        </el-row>
      <!-- 表格 -->
      <el-table
      :data="goodsList"
      border
      stripe
      style="width: 100%">
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column prop="goods_price" label="商品价格(元)" width="100"></el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
      <el-table-column label="添加时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.add_time | formatTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
           <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
           <el-button @click="removeGood(scope.row.goods_id)" type="danger" size="mini" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->
         <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryData.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询参数
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 列表渲染数据
      goodsList: [],
      // 数据总条数
      total: 0
    }
  },

  created () {
    this.getGoodsList()
  },

  methods: {
    // 获取商品数据
    async getGoodsList () {
      const { data: res } = await this.$axios.get('goods', { params: this.queryData })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 每页几条数据发生变化时重新加载
    handleSizeChange (newSize) {
      this.queryData.pagesize = newSize
      this.getGoodsList()
    },
    // 查询第几页数据是重新加载
    handleCurrentChange (newNum) {
      this.queryData.pagenum = newNum
      this.getGoodsList()
    },
    // 删除商品
    removeGood (id) {
      this.$confirm('是否删除该用户数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete(`goods/${id}`)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.getGoodsList()
        return this.$message.success('删除成功!')
      }).catch(() => {
        return this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
